package com.wangsd.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.common.enums.CommonStatic;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.Merchant;
import com.wangsd.web.coupon.entity.ServiceBusiness;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.service.CompanyService;
import com.wangsd.web.coupon.service.MerchantService;
import com.wangsd.web.coupon.service.ServiceBusinessService;
import com.wangsd.web.coupon.service.StoreService;
import com.wangsd.web.sys.entity.SysMenu;
import com.wangsd.web.sys.entity.SysRole;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.entity.SysUserRole;
import com.wangsd.web.sys.mapper.SysUserMapper;
import com.wangsd.web.sys.service.SysMenuService;
import com.wangsd.web.sys.service.SysRoleService;
import com.wangsd.web.sys.service.SysUserRoleService;
import com.wangsd.web.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-02
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    ServiceBusinessService serviceBusinessService;
    @Autowired
    CompanyService companyService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    StoreService storeService;


    @Override
    public IPage<SysUser> selectList(String code, Integer pageNum, Integer pageSize, SysUser sysUser) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        if (sysUser.getType() == 1) {
            return sysUserMapper.selectListBusinessXml(page, code, sysUser);
        } else if (sysUser.getType() == 2) {
            return sysUserMapper.selectListAgentXml(page, code, sysUser);
        } else if (sysUser.getType() == 3) {
            return sysUserMapper.selectListMerchantXml(page, code, sysUser);
        } else if (sysUser.getType() == 4) {
            return sysUserMapper.selectListStoreXml(page, code, sysUser);
        } else {
            return sysUserMapper.selectListXml(page, code, sysUser);
        }
//        return sysUserMapper.selectListXml(page, code, sysUser);
    }

    @Override
    public IPage<SysUser> selectListDemo(Integer pageNum, Integer pageSize, SysUser sysUser) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        return sysUserMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public Integer addOrUpdate(SysUser sysUser) {
        if (sysUser.getId() != null) {
            QueryWrapper<SysUserRole> query = new QueryWrapper<SysUserRole>().eq("user_id", sysUser.getId());
            sysUserRoleService.remove(query);
            sysUserMapper.updateById(sysUser);
        } else {
            sysUserMapper.insert(sysUser);
            String code = "";
            switch (sysUser.getType()) {
                case 1://服务商用户
                    ServiceBusiness serviceBusiness = serviceBusinessService.getById(sysUser.getParentId());
                    code = serviceBusiness.getCode();
                    break;
                case 2://商圈用户
                    Company agent = companyService.getById(sysUser.getParentId());
                    code = agent.getCode();
                    break;
                case 3://商户用户
                    Merchant merchant = merchantService.getById(sysUser.getParentId());
                    code = merchant.getCode();
                    break;
                case 4://门店用户
                    Store store = storeService.getById(sysUser.getParentId());
                    code = store.getCode();
                    break;
            }
            sysUser.setCode(code);
            sysUserMapper.updateById(sysUser);
        }

        if (sysUser.getRoleIds() != null) {
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            for (Long roleId : sysUser.getRoleIds()) {
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(sysUser.getId());
                userRole.setRoleId(roleId);
                sysUserRoleList.add(userRole);
            }
            if (sysUserRoleList.size() > 0) {
                sysUserRoleService.saveBatch(sysUserRoleList);
            }
        }
        return 1;
    }

    @Override
    public SysUser selectByUsernameOrId(String username, Long id) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser selectLogin(String username, String password) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        queryWrapper.eq("deleted", 0);
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser selectPermission(SysUser user) {
        List<SysMenu> menuList = new ArrayList<>();
        if (user.getType() == CommonStatic.IS_SUPER_MANAGER) {//超级管理员
            menuList = sysMenuService.selectByAdmin();
        } else {
            List<SysRole> roleList = sysRoleService.selectByUserId(user.getId());
            for (SysRole role : roleList) {
                List<SysMenu> temp = sysMenuService.selectByRoleId(role.getId());
                menuList.addAll(temp);
            }
        }
        List<String> keySing = menuList.stream().map(SysMenu::getKeySign).collect(Collectors.toList());
        user.setMenuList(menuList);
        user.setPermissionList(keySing);
        return user;
    }
}
